package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.ScopeUseEnum;
import com.member.common.utils.DateUtils;
import com.member.common.utils.OrderNoCreate;
import com.member.shop.domain.*;
import com.member.shop.dto.SysBizGoodsDto;
import com.member.shop.mapper.SysBizCouponMapper;
import com.member.shop.service.ISysBizCouponGoodsService;
import com.member.shop.service.ISysBizCouponService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 优惠券规则Service业务层处理
 *
 * @author lixinchen
 * @date 2024-05-08
 */
@Service
public class SysBizCouponServiceImpl extends ServiceImpl<SysBizCouponMapper, SysBizCoupon> implements ISysBizCouponService {
    @Resource
    private SysBizCouponMapper sysBizCouponMapper;
    @Resource
    private ISysBizCouponGoodsService sysBizCouponGoodsService;

    /**
     * 查询优惠券规则
     *
     * @param id 优惠券规则主键
     * @return 优惠券规则
     */
    @Override
    public SysBizCoupon selectSysBizCouponById(Long id) {
        SysBizCoupon sysBizCoupon = sysBizCouponMapper.selectById(id);
        if(Objects.equals(sysBizCoupon.getScopeUse(), ScopeUseEnum.SPECIFY_GOODS.getKey())){
            List<SysBizGoodsDto> sysBizGoods =sysBizCouponGoodsService.selectSysBizCouponGoodsByCouponId(sysBizCoupon.getId());
            sysBizCoupon.setSysBizGoods(sysBizGoods);
        }
        return sysBizCoupon;
    }

    /**
     * 查询优惠券规则列表
     *
     * @param queryWrapper 优惠券规则
     * @return 优惠券规则
     */
    @Override
    public List<SysBizCoupon> selectSysBizCouponList(LambdaQueryWrapper<SysBizCoupon> queryWrapper) {
        return sysBizCouponMapper.selectList(queryWrapper);
    }

    /**
     * 新增优惠券规则
     *
     * @param sysBizCoupon 优惠券规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysBizCoupon(SysBizCoupon sysBizCoupon) {
        sysBizCoupon.setCreateTime(DateUtils.getNowDate());
        sysBizCoupon.setUid(OrderNoCreate.couponCode());
        sysBizCoupon.setSurplusNum(sysBizCoupon.getTotalNum());
        if (sysBizCoupon.getValidityType() == 1) {
            sysBizCoupon.setTermEndTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, sysBizCoupon.getTermEndTime()) + " 23:59:59"));
            sysBizCoupon.setReceivingDay(null);
            sysBizCoupon.setEffectiveDay(null);
        }
        if (sysBizCoupon.getValidityType() == 2) {
            sysBizCoupon.setTermBeginTime(null);
            sysBizCoupon.setTermEndTime(null);
        }
        int insert = sysBizCouponMapper.insert(sysBizCoupon);

        sysBizCouponGoodsService.deleteSysBizCouponGoodsBycouponId(sysBizCoupon.getId());
        if (sysBizCoupon.getScopeUse().equals(ScopeUseEnum.SPECIFY_GOODS.getKey()) && !sysBizCoupon.getSysBizCouponGoods().isEmpty()) {
            for (SysBizCouponGoods sysBizCouponGood : sysBizCoupon.getSysBizCouponGoods()) {
                sysBizCouponGood.setCouponId(sysBizCoupon.getId());
            }
            sysBizCouponGoodsService.saveBatch(sysBizCoupon.getSysBizCouponGoods());
        }

        return insert;
    }

    /**
     * 修改优惠券规则
     *
     * @param sysBizCoupon 优惠券规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysBizCoupon(SysBizCoupon sysBizCoupon) {
        sysBizCoupon.setUpdateTime(DateUtils.getNowDate());

        //编辑修改优惠券总数
        SysBizCoupon sysBizCoupon1 = sysBizCouponMapper.selectSysBizCouponById(sysBizCoupon.getId());
        if(!sysBizCoupon.getTotalNum().equals(sysBizCoupon1.getTotalNum())){
            sysBizCoupon.setSurplusNum(sysBizCoupon.getTotalNum() - sysBizCoupon.getIssueNum());
//            sysBizCoupon.setIssueNum(0L);
//            sysBizCoupon.setUsedNum(0L);
        }


        if (sysBizCoupon.getValidityType() == 1) {
            sysBizCoupon.setTermEndTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, sysBizCoupon.getTermEndTime()) + " 23:59:59"));
            sysBizCoupon.setReceivingDay(null);
            sysBizCoupon.setEffectiveDay(null);
        }
        if (sysBizCoupon.getValidityType() == 2) {
            sysBizCoupon.setTermBeginTime(null);
            sysBizCoupon.setTermEndTime(null);
        }
        int i = sysBizCouponMapper.updateById(sysBizCoupon);
        sysBizCouponGoodsService.deleteSysBizCouponGoodsBycouponId(sysBizCoupon.getId());
        if (sysBizCoupon.getScopeUse().equals(ScopeUseEnum.SPECIFY_GOODS.getKey()) && !sysBizCoupon.getSysBizCouponGoods().isEmpty()) {
            for (SysBizCouponGoods sysBizCouponGood : sysBizCoupon.getSysBizCouponGoods()) {
                sysBizCouponGood.setCouponId(sysBizCoupon.getId());
            }
            sysBizCouponGoodsService.saveBatch(sysBizCoupon.getSysBizCouponGoods());
        }

        return i;
    }

    /**
     * 批量删除优惠券规则
     *
     * @param ids 需要删除的优惠券规则主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponByIds(Long[] ids) {
        return sysBizCouponMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除优惠券规则信息
     *
     * @param id 优惠券规则主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponById(Long id) {
        SysBizCoupon sysBizCoupon = new SysBizCoupon();
        sysBizCoupon.setId(id);
        sysBizCoupon.setDeleted(OrderDeleteEnum.DELETE.getKey());
        return sysBizCouponMapper.updateById(sysBizCoupon);
    }

    @Override
    public Long getUsedNumCoupon(Long id) {
        return sysBizCouponMapper.getUsedNumCoupon(id);
    }

    @Override
    public List<SysBizCoupon> list2(SysBizCoupon sysBizCoupon) {
        return sysBizCouponMapper.list2(sysBizCoupon);
    }
}
